package com.gf.gateway.common;

import java.io.Serializable;

/**
 * @author gFly
 * @date 2024/1/12 15:37
 * @Describe
 */
public final class R<T> implements Serializable {
    private T data;
    private Object code;
    private String msg;

    public static <T> R<T> succeed(T entity) {
        return new R(entity, 0, "");
    }

    public static <T> R<T> succeed(Object code, String message) {
        return new R((Object)null, code, message);
    }

    public static <T> R<T> failed() {
        return new R((Object)null, CommonErrors.SYSTEM_ERROR.code(), CommonErrors.SYSTEM_ERROR.description(new Object[0]));
    }

    public static <T> R<T> failed(Object code, String message) {
        return new R((Object)null, code, message);
    }

    public static <T> R<T> failed(String message) {
        return failed(CommonErrors.SYSTEM_ERROR.code(), message);
    }

    public static <T> R<T> failed(StandardException cause) {
        return new R((Object)null, cause.errorCode(), cause.errorMessage());
    }

    public T getData() {
        return this.data;
    }

    public Object getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public R() {
    }

    public R(T data, Object code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }
}
